package com.sanpu.kxe.business.main.presenter;

import com.sanpu.kxe.business.main.bean.PushDetailsOutput;
import com.sanpu.kxe.business.main.contract.PushListContract;
import com.sanpu.kxe.business.main.model.MainModel;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：地推物料列表
 * Created by 薛勇军 on 2018/8/22.
 */

public class PushListPresenter implements PushListContract.Presenter {

    private PushListContract.View mView;

    private CompositeSubscription mSubscription;

    private String mCurrentType;

    public PushListPresenter(PushListContract.View mView, String materialType) {
        this.mView = mView;
        this.mCurrentType = materialType;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getMaterialDetailsList(final int nowPage) {
        Subscription subscription = MainModel.getInstance().getMaterialDetailsList(nowPage, mCurrentType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PushDetailsOutput>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showErrorText(e);
//                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                        mView.showPushListView(null, nowPage == 0);
                    }

                    @Override
                    public void onNext(List<PushDetailsOutput> pushDetailsOutputs) {
                        mView.showPushListView(pushDetailsOutputs, nowPage == 0);
//                        if (pushDetailsOutputs != null) {
//                            if (pushDetailsOutputs.size() < CommonConfig.PAGENUMER) {
//                                mView.showLoadMore(CommonConfig.LOADMOREEND);
//                            } else {
//                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
//                            }
//                        } else {
//                            mView.showLoadMore(CommonConfig.LOADMOREEND);
//                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {mView = null;}
    }
}
